//输入补全提示
define(["inputsug.css", "series.js"], function(series) {
    var scope = this;
    var cls = scope.cls = {
        ul: 'bd-sug-list',
        li: 'bd-sug-item',
        hover: 'bd-sug-hover'
    };
    var contentCache = {};
    function getContent(s){
        return contentCache[s.name] || (contentCache[s.name] = [s.name].concat(s.alias).join(" ").toLowerCase());
    }
    scope.getSuggestion = function(name){
        return series.filter(function(item){
            return getContent(item).indexOf(name.toLowerCase()) != -1;
        }).slice(0,10).map(function(item){
            return item.name;
        });
    }
    scope.emptySug = true;
    
    scope.init = function($input){
        var $$input = $$.s($input[0], "keydown");   //用户输入
        var $ul = $$('<ul class="'+cls.ul+'" style="display:none"><li bd-repeat="item in $$sugList" bd-class="{{cursor==__i?'+"'"+cls.hover+"':''"+'}}" bd-click="setInput(__i)" class="'+cls.li+'">{{item}}</li></ul>');
        var inputWidget = $$.rootWidget.load("", $ul[0]);
        var inputScope = inputWidget.scope;
        var $$sugList = inputScope.$$sugList = //提示列表
            $$.s($input[0], "input focus").map(function(e){
                var word = e.target.value;
                if(word || scope.emptySug){
                    return scope.getSuggestion(word);
                }else{
                    return null;
                }
            });
        $$sugList.filter(function(e){
            return !e;
        }).on(hideList);
        inputScope.cursor = -1;
        $ul.insertAfter($input);
        scope.isActive = function(){
            return inputScope.cursor != -1;
        };
        function upOrDown(type) {
            var sugList = $$sugList.fetch();
            if(!sugList || !sugList.length) return;
            if (type == "up") {
                if (inputScope.cursor <= -1) {
                    inputScope.cursor = sugList.length - 1;
                } else {
                    inputScope.cursor--;
                }
            } else if (type == "down") {
                if (inputScope.cursor >= sugList.length - 1) {
                    inputScope.cursor = -1;
                } else {
                    inputScope.cursor++
                }
            }
            inputScope.$refresh();
        }
        function showList(){
            var input = $input[0];
            var parent = input.parentNode;
            if(parent.tagName.toLowerCase() == 'td'){
                parent.style.position = 'relative';
            }
            $ul.css({
                position: 'absolute',
                left: input.offsetLeft,
                top: input.offsetTop + input.offsetHeight,
                width: scope.state.width || input.offsetWidth
            }).show();
        }
        function hideList(){
            setTimeout(function(){ //delay to ensure setInput available
                inputScope.cursor = -1;
                $ul.hide();
            }, 200);
        }
        inputScope.setInput = function(i){
            if($$.isNumber(i)){
                inputScope.cursor = i;
            }
            var sugList = $$sugList.fetch();
            if(sugList && sugList[inputScope.cursor]){
                $input.val(sugList[inputScope.cursor]);
            }
            $$sugList.push(null);
            $input.focus();
        }

        function key(code){
            return $$input.filter(function(e){
                return code == e.keyCode;
            });
        }
        key(38).map("up").merge(key(40).map("down")).on(upOrDown); //上下导航
        key(13).merge(key(9)).on(inputScope.setInput);
        $input.bind("blur", hideList);
        
        $$sugList.filter(function(list){ //当有输入提示项时显示
            return list && list.length;
        }).on(showList);

    }
})
